{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
    <div class="layui-card-header">数据转换</div>
    <div class="layui-card-body">
        <blockquote class="layui-elem-quote">
            本插件不会对dedecms进行任何数据修改，但是会清空yzncms的cms模块和附件的数据，请先对yzncms系统进行备份！！！<br>
            插件详细使用教程：<a href="https://www.kancloud.cn/ken678/yzncms/2483133" target="_blank">https://www.kancloud.cn/ken678/yzncms/2483133</a>
        </blockquote>
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label class="layui-form-label">服务器地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="hostname" lay-verify="required" autocomplete="off" placeholder="服务器地址" class="layui-input" value="127.0.0.1">
                </div>
                <div class="layui-form-mid layui-word-aux">dedecms的服务器地址</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">数据库名</label>
                <div class="layui-input-inline">
                    <input type="text" name="database" lay-verify="required" autocomplete="off" placeholder="数据库名" class="layui-input" value="dedecms">
                </div>
                <div class="layui-form-mid layui-word-aux">dedecms的数据库名</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" lay-verify="required" autocomplete="off" placeholder="数据库用户名" class="layui-input" value="root">
                </div>
                <div class="layui-form-mid layui-word-aux">dedecms的数据库用户名</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="text" name="password" lay-verify="required" autocomplete="off" placeholder="数据库密码" class="layui-input" value="root">
                </div>
                <div class="layui-form-mid layui-word-aux">dedecms的数据库密码</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">表前缀</label>
                <div class="layui-input-inline">
                    <input type="text" name="prefix" lay-verify="required" autocomplete="off" placeholder="数据库表前缀" class="layui-input" value="dede_">
                </div>
                <div class="layui-form-mid layui-word-aux">dedecms的数据库表前缀</div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit>开始转换</button>
                </div>
            </div>
            <div id="change_div" style="display: none;padding: 20px;background: #f2f2f2;">
                <div class="scroller" style="height:250px" id="change_html"></div>
            </div>
        </form>
    </div>
</div>
{/block}
{block name="script"}
<script type="text/javascript">
layui.use(['form', 'yzn', 'layer'], function() {
    var form = layui.form,
        layer = layui.layer,
        yzn = layui.yzn;

    form.on('submit', function(data) {
        $('.layui-btn').addClass('layui-btn-disabled').prop('disabled', true).html('<i class="layui-icon layui-icon-loading layui-icon layui-anim layui-anim-rotate layui-anim-loop"></i> 转换中...');
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "{:url('addons/dedetoyzn/init', ['isadmin' => 1])}",
            data: data.field,
            success: function(res) {
                if (res.code == 1) {
                    $('#change_div').show();
                    $('#change_html').html('<p>'+res.msg+'</p>');
                    start_change(0);
                } else {
                    $('.layui-btn').attr('disabled', false).removeClass("layui-btn-disabled").html('开始转换');
                    layer.alert(res.msg, { icon: 2 });
                }
            },
            error: function(xhr, textstatus, thrown) {

            }
        });
        return false;
    });
});

function start_change(page) {
    $.ajax({
        type: "GET",
        dataType: "json",
        url: "{:url('addons/dedetoyzn/start', ['isadmin' => 1])}?page=" + page,
        success: function(res) {
            if (res.code == 1) {
                if (res.data.code == 1) {
                    start_change(res.data.page);
                    $('#change_html').append('<p>'+res.msg+'</p>');
                } else if (res.data.code == 2) {
                    $('.layui-btn').attr('disabled', false).removeClass("layui-btn-disabled").html('开始转换');
                    layer.alert(res.msg, { icon: 1 });
                } else {

                }
            } else {
                layer.alert(res.msg, { icon: 2 });
                $('.layui-btn').attr('disabled', false).removeClass("layui-btn-disabled").html('重新转换');
            }
        },
        error: function(xhr, textstatus, thrown) {

        }
    });
}
</script>
{/block}